package com.tuanbiao.presenter;

import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.OnLifecycleEvent;

import com.abiao.lib_utils.IntentUtils;
import com.hzjq.data.callback.ProgressHttpCallback;
import com.hzjq.data.hzjq.entity.AppDataEntity;
import com.tuanbiao.AppConfig;
import com.tuanbiao.JQBlastConfig;
import com.tuanbiao.base.mvp.BasePresenter;
import com.tuanbiao.controllers.AboutController;
import com.tuanbiao.data.callback.OnBaseResultCallback;
import com.tuanbiao.data.callback.OnProgressCallback;
import com.tuanbiao.models.AboutModel;
import com.tuanbiao.utils.SettingUtils;

import java.io.File;

import timber.log.Timber;

/**
 * @author abiao
 * @date 2022/7/4 9:05
 * @description:
 */
public class AboutPresenter extends BasePresenter<AboutModel, AboutController.View> implements AboutController.Presenter {
    public AboutPresenter(AboutModel model, AboutController.View rootView) {
        super(model, rootView);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void init() {
        mModel.getAppVersion(versionResult -> {
            mRootView.showVersion("v_" + versionResult.getFirmwareVersion(),"v_" + versionResult.getAppVersion(),AppConfig.getBuildType());
            if (TextUtils.isDigitsOnly(versionResult.getFirmwareVersion())) {
                mRootView.showFirmwareUpdate(JQBlastConfig.getBinTagVersion() > Integer.parseInt(versionResult.getFirmwareVersion()));
            }
        });
        mModel.loadApp(false, entity -> {
            Timber.i("loadApp#" + entity);
            mRootView.showAppUpdate(SettingUtils.compareVersion(entity.getVersioncode(), AppConfig.getVersionName()));
        });
        mRootView.showBuildTime(AppConfig.getApkBuildTime());
    }

    @Override
    public void updateFirmware() {
        mModel.upgrade(new OnProgressCallback<Integer>() {

            @Override
            public void onProgress(int progress, String msg) {
                Timber.i("onProgress" + progress + ",msg:" + msg);
                mRootView.showProgressDialog(progress, msg);
            }

            @Override
            public void onFailed(String msg) {
                mRootView.updateFailed(msg);
            }

            @Override
            public void onSuccess(Integer version) {

            }

            @Override
            public void onAfter() {
                mRootView.updateFinish();
            }
        });
    }

    @Override
    public void checkAppUpdate() {
        Timber.i("update");
        mModel.loadApp(false, new OnBaseResultCallback<AppDataEntity>() {
            @Override
            public void onSuccess(AppDataEntity entity) {
                Timber.i("update:" + entity);
                if (SettingUtils.compareVersion(entity.getVersioncode(), AppConfig.getVersionName())) {
                    mRootView.showAppUpdateDialog(entity);
                } else {
                    mRootView.showMessage("已经是最新版本了");
                }
//                if (TextUtils.isDigitsOnly(entity.getVersioncode())) {
//                    if (Integer.parseInt(entity.getVersioncode()) != AppConfig.getVersionCode()) {
//                        mRootView.showUpdateDialog(entity);
//                    } else {
//                        mRootView.showMessage("已经是最新版本了");
//                    }
//                } else {
//                    Timber.i("update");
//                }
            }

            @Override
            public void onFailed(String msg) {
                mRootView.showMessage(msg);
            }
        });
    }

    @Override
    public void downloadApk(AppDataEntity entity) {
        mModel.downLoadApk(entity, new ProgressHttpCallback<File>() {
            @Override
            public void onProgressChanged(int i, int i1) {
                Timber.i("downloadApk#onProgressChanged#i:" + i + ",i1:" + i1);
            }

            @Override
            public void onSuccess(File file) {
                Timber.i("downloadApk#onSuccess:" + file);
                mRootView.startInstall(IntentUtils.getInstallAppIntent(file));
            }

            @Override
            public void onFailed(@NonNull String s) {
                Timber.i("downloadApk#s:" + s);
                mRootView.showMessage(s);
            }
        });
    }
}
